package com.zjmzxfzhl.modules.app.service.impl;

import com.zjmzxfzhl.modules.app.entity.*;
import com.zjmzxfzhl.modules.app.form.AppStockVo;
import com.zjmzxfzhl.modules.app.form.DocFilePath;
import com.zjmzxfzhl.modules.app.form.StockSumVo;
import com.zjmzxfzhl.modules.app.service.AppDocService;
import com.zjmzxfzhl.modules.app.service.AppStockUserService;
import com.zjmzxfzhl.modules.sys.entity.SysUser;
import org.flowable.common.engine.api.FlowableException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.mapper.AppStockMapper;
import com.zjmzxfzhl.modules.app.service.AppStockService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppStockServiceImpl extends BaseServiceImpl<AppStockMapper, AppStock>
        implements AppStockService {
    @Autowired
    private AppStockUserService appStockUserService;
    @Autowired
    private AppDocService appDocService;

    @Override
    public IPage<AppStock> list(IPage<AppStock> page, AppStockVo appStockVo) {
        return page.setRecords(baseMapper.list(page, appStockVo));
    }
    public StockSumVo getStockSum(AppStock appStock) {
        return baseMapper.getStockSum(appStock);
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appStock
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppStock(AppStock appStock) {
        //保存产品成员数组，标签数组
        List<SysUser> userList=null;
        List<DocFilePath> docFilePaths= null;
        if(appStock.getUserList()!=null){
            userList=appStock.getUserList();
        }
        if(appStock.getDocFilePath()!=null){
            docFilePaths=appStock.getDocFilePath();
        }
        //添加项目
        appStock =setAppStock(appStock);
        Boolean result = this.save(appStock);
        if(result==false){
            return result;
        }
        appStock=this.getById(appStock.getStockId());

        //添加项目成员，项目标签
        if(userList!=null){
            for (SysUser user:userList) {
                AppStockUser appStockUser=new AppStockUser();
                appStockUser.setStockId(appStock.getStockId());
                appStockUser.setUserId(user.getUserId());
                appStockUserService.saveAppStockUser(appStockUser);
            }
        }
        //添加物料库存附件
        if (docFilePaths != null && docFilePaths.size() > 0) {
            AppDoc appDoc=new AppDoc();
            appDoc.setDocFilePath(docFilePaths);
            appDoc.setDeleted("0");
            appDoc.setName("物料库存附件");
            appDoc.setDirId("##");
            appDoc.setDocType("07");
            appDoc.setDocTypeId(appStock.getStockId());
            appDoc.setContent("物料库存附带文件");
            appDocService.saveAppDoc(appDoc);
        }
        return result;
    }

    private AppStock setAppStock(AppStock appStock){

        return appStock;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppStock queryById(String id) {
        AppStockVo appStockVo=new AppStockVo();
        appStockVo.setStockId(id);
        List<AppStock> appStockList= baseMapper.list(null, appStockVo);
        if(appStockList.size()>0){
            return appStockList.get(0);
        }
        return null;
    }

    /**
     * 新增机构，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appStock
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAppStock(AppStock appStock) {
        //保存修改标签数组
        List<SysUser> userList=null;
        if(appStock.getUserList()!=null){
            userList=appStock.getUserList();
        }
        appStock.setUpdateTime(new Date());
        Boolean result = this.updateById(appStock);
        if(result==false){
            return false;
        }

        //删除标签
        //添加标签
        if(userList!=null){
            AppStockUser appStockUser=new AppStockUser();
            appStockUser.setStockId(appStock.getStockId());
            appStockUserService.relationDel(appStockUser);
            for (SysUser user:userList) {
                appStockUser.setUserId(user.getUserId());
                appStockUserService.saveAppStockUser(appStockUser);
            }
        }
        return result;
    }

}
